﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using HZ.DrugTrade.Model;
using HZ.DrugTrade.IBLL;
using HZ.DrugTrade.BLLFactory;
using HZ.DrugTrade.Model.Enum;
using System.Data;
namespace HZ.DrugTrade.Web.Admin.BidProdManage
{
    public partial class BidInvalidProductList : BasePage
    {
        IBLL_ProcurementBasic pbCtrl = Factory.BLL_ProcurementBasic();
        IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
        public BidInvalidProductList()
        {
            this.ModuleNo = Module.采购产品管理.ToString();
            this.PageOperateType = OperateType.采购产品列表.ToString();
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //绑定所有项目
                ProcurementProject procurementProject = new ProcurementProject();
                procurementProject.ProcureStartTimeFrom = Convert.ToDateTime("1890-01-01");
                procurementProject.ProcureStartTimeTo = Convert.ToDateTime("2099-12-31");
                procurementProject.ProcureEndTimeFrom = Convert.ToDateTime("2000-01-01");
                procurementProject.ProcureEndTimeTo = Convert.ToDateTime("2099-12-31");
                int temp;
                IList<ProcurementProject> projectList = ipb.GetProjectByWhere(procurementProject, 1, 0, out temp);
                ddlProject.DataSource = projectList;
                ddlProject.DataTextField = "ProjectName";
                ddlProject.DataValueField = "ProjectID";
                ddlProject.DataBind();
                ListItem item = new ListItem();
                item.Text = Constants.ALL_ITEM_TEXT;
                item.Value = Constants.ALL_ITEM_VALUE;
                ddlProject.Items.Insert(0, item);
                //控件显示控制
                if (!CurrentAccount.GetUser().Org.Area.AreaID.Contains("0000"))
                {
                    
                    this.btnAdd.Visible = false;
                    this.btnBatchAdd.Visible = false;
                    this.btnAbandon.Visible = false;
                    this.btnRecover.Visible = false;

                }
                setBind();
            }
        }

        private void setBind()
        {
            //获取查询条件
            Model.Query.ProcureCatalog procureCatalog = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            procureCatalog.CompanyName_SC = txtProduceCompany_SC.Text.Trim();
            //procureCatalog.PinyinCode = this.txtPinyinCode.Text.Trim().ToUpper();
            procureCatalog.Material = txtMaterial.Text.Trim();
            procureCatalog.Outlookc = txtOutLook.Text.Trim();
            procureCatalog.ProjectID = ddlProject.SelectedItem.Value;
            procureCatalog.GoodsName = txtGoodsName.Text.Trim();

            //if (HZ.Common.MyValidate.IsNumeric(txtFactor.Text.Trim()))
            //{
            //    procureCatalog.Factor = Convert.ToInt32(txtFactor.Text.Trim());
            //}
            procureCatalog.CompanyName_TB = txtCompany_TB.Text;
            procureCatalog.SortFields = " GoodsName asc";

            if (!string.IsNullOrEmpty(txtAddTimeFrom.Text.Trim()))
            {
                procureCatalog.AddTimeFrom = Convert.ToDateTime(txtAddTimeFrom.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtAddTimeTo.Text.Trim()))
            {
                procureCatalog.AddTimeTo = Convert.ToDateTime(txtAddTimeTo.Text.Trim());
            }
            
            //调用方法，执行查询，分页显示产品信息
            //AspNetPager1.PageSize = Constants.GRIDVIEW_PAGE_SIZE;
            int totalCount = 0;
            IBLL_ProcurementBasic ipb = Factory.BLL_ProcurementBasic();
            try
            {

                IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetInvalidProcureCatalogByWhere(procureCatalog, AspNetPager1.CurrentPageIndex, AspNetPager1.PageSize, out totalCount);
                AspNetPager1.RecordCount = totalCount;
                gvwProduceCatalog.DataSource = procureCatalogList;
                gvwProduceCatalog.DataBind();
            }
            catch (Exception ex)
            {
                PageError(ex.Message, "");
            }
        }


        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnQuery_Click(object sender, EventArgs e)
        {
            AspNetPager1.CurrentPageIndex = 1;
            setBind();
        }

        /// <summary>
        /// 翻页
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void AspNetPager1_OnPageChanged(object sender, EventArgs e)
        {
            setBind();
        }


        /// <summary>
        /// 撤销
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnAbandon_Click(object sender, EventArgs e)
        {
            string ids = Request["checkbox1"];

            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    Model.View.ProcureCatalog tmpProcureCatalog = pbCtrl.GetProcureCatalogByID(Item);
                    if (Convert.ToBoolean(tmpProcureCatalog.IsUsing))
                    {
                        ProcureCatalog aProcureCatalog = new ProcureCatalog();
                        aProcureCatalog.IsUsing = false;
                        aProcureCatalog.ProcureCatalogID = tmpProcureCatalog.ProcureCatalogID;
                        int nSuccess = pbCtrl.UpdateProcureCatalog_IsUsing(aProcureCatalog);
                        if (nSuccess > 0)
                        {
                            View_Procurecatalog_TB vpt = new View_Procurecatalog_TB();
                            vpt.IsUsing = false;
                            vpt.GoodsID = tmpProcureCatalog.ProcureCatalogID;
                            int success = pbCtrl.UpdateProcureCatalog_IsUsingZDC(vpt);
                        }
                        if (nSuccess == 0)
                        {
                            PageError("数据操作失败！", "");
                        }
                    }
                }
            }
            setBind();
        }

        /// <summary>
        /// 恢复
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnRecover_Click(object sender, EventArgs e)
        {
            string ids = Request["checkbox1"];

            if (!string.IsNullOrEmpty(ids))
            {
                string[] arraryIds = ids.Split(',');
                foreach (string Item in arraryIds)
                {
                    Model.View.ProcureCatalog tmpProcureCatalog = pbCtrl.GetProcureCatalogByID(Item);
                    if (!Convert.ToBoolean(tmpProcureCatalog.IsUsing))
                    {
                        ProcureCatalog aProcureCatalog = new ProcureCatalog();
                        aProcureCatalog.IsUsing = true;
                        aProcureCatalog.ProcureCatalogID = tmpProcureCatalog.ProcureCatalogID;
                        int nSuccess = pbCtrl.UpdateProcureCatalog_IsUsing(aProcureCatalog);
                        if (nSuccess > 0)
                        {
                            View_Procurecatalog_TB vpt = new View_Procurecatalog_TB();
                            vpt.IsUsing = true;
                            vpt.GoodsID = tmpProcureCatalog.ProcureCatalogID;
                            int success = pbCtrl.UpdateProcureCatalog_IsUsingZDC(vpt);
                        }
                        if (nSuccess == 0)
                        {
                            PageError("数据操作失败！", "");
                        }
                    }
                }
            }
            setBind();
        }

        /// <summary>
        /// 简单查询与高级查询之间的转换
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lbnSwitch_Click(object sender, EventArgs e)
        {
            if (lbnSwitch.CommandArgument == "simple")
            {
                ltlAdvance.Text = "";
                lbnSwitch.CommandArgument = "advance";
                lbnSwitch.Text = "简单查询";
            }
            else
            {
                ltlAdvance.Text = "<tbody id='advance' style='display:none'>";
                lbnSwitch.CommandArgument = "simple";
                lbnSwitch.Text = "高级查询";

                //清空数据
                txtAddTimeFrom.Text = "";
                txtAddTimeTo.Text = "";
                txtCompany_TB.Text = "";
                //txtPinyinCode.Text = "";
                txtProduceCompany_SC.Text = "";
                txtUnit.Text = "";
                ddlProject.SelectedIndex = -1;

            }
        }
        /// <summary>
        /// 导出无效品种 2009-11-14
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnExport_Click(object sender, EventArgs e)
        {
            HZ.DrugTrade.Model.Query.ProcureCatalog model = new HZ.DrugTrade.Model.Query.ProcureCatalog();
            model.GoodsName = this.txtGoodsName.Text.Trim();
            model.Material = this.txtMaterial.Text.Trim();
            model.Outlookc = this.txtOutLook.Text.Trim();
            //model.PinyinCode = this.txtPinyinCode.Text.Trim();
            model.CompanyName_TB = this.txtCompany_TB.Text.Trim();
            model.CompanyName_SC = this.txtProduceCompany_SC.Text.Trim();
            model.Unit = this.txtUnit.Text.Trim();
            model.ProjectName = this.ddlProject.SelectedItem.Value.ToString();
            if (!string.IsNullOrEmpty(txtAddTimeFrom.Text.Trim()))
            {
                model.AddTimeFrom = Convert.ToDateTime(this.txtAddTimeFrom.Text.Trim());
            }
            if (!string.IsNullOrEmpty(txtAddTimeTo.Text.Trim()))
            {
                model.AddTimeTo = Convert.ToDateTime(this.txtAddTimeTo.Text.Trim());
            }
            //if (ucSource1.SelectValue != -1) model.SortSource = ucSource1.SelectValue.ToString();
            //if (ucIsBasicRemedy1.SelectValue != -1) model.IsBasicRemedy = ucIsBasicRemedy1.SelectValue;
            model.SortFields = " GoodsName asc";
            int totalcount = 0;
            IList<Model.View.ProcureCatalog> procureCatalogList = ipb.GetInvalidProcureCatalogByWhere(model, 1, 0, out totalcount);
             DataTable dt = HZ.Data.ModelConvert.GetDataTableFromModel(procureCatalogList);
            HZ.Excel.ExcelHelper.ExportDataForWeb(dt, gvwProduceCatalog);
        }
       
    }
}
